using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Yoozoo.Gameplay.RTS;

namespace Gameplay.RTS.Editor
{
    public class RTSEffectHelperEditorWindow: EditorWindow
    {
        private Vector2 pos = new Vector2(0, 0);

        [MenuItem("GTATools/GamePlay/RTS/RTS普攻特效工具", false, 0)]
        private static void OpenParticleHelperWindow()
        {
            RTSEffectHelperEditorWindow win = GetWindow<RTSEffectHelperEditorWindow>("RTS普攻特效工具");
            win.Show();
        }

        private void OnGUI()
        {
            pos = EditorGUILayout.BeginScrollView(pos);
            if (GUILayout.Button("重载攻击特效配置"))
            {
                ArmyManager.Instance.ResourceManager.JsonManager.PreloadSoldierEffectParam((() =>
                {
                    RTSTest.ReLoadSoldierEffectConfig();
                    EditorUtility.DisplayDialog("提示", "重载配置成功","确认");
                }));

                ArmyManager.Instance.ResourceManager.JsonManager.ReloadConfigAll();
            }

            if (GUILayout.Button("生成RTS普工特效代码文件"))
            {
                //GenerateSoldierEffectPathList();
            }

            
            EditorGUILayout.EndScrollView();
        }

        private void GenerateSoldierEffectPathList()
        {
            CodeCompileUnit unit = new CodeCompileUnit();
            //命名空间
            CodeNamespace codeNamespace = new CodeNamespace("Yoozoo.Gameplay.RTS");
            //引用
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            
            CodeTypeDeclaration rtsResourceManager = new CodeTypeDeclaration("RTSResourceManager");
            rtsResourceManager.IsClass = true;
            rtsResourceManager.TypeAttributes = TypeAttributes.Public;
            rtsResourceManager.IsPartial = true;

            codeNamespace.Types.Add(rtsResourceManager);
            unit.Namespaces.Add(codeNamespace);
            
            
            CodeMemberField field = new CodeMemberField(typeof(List<string>),"soldierEffectPathList");
            field.Attributes = MemberAttributes.Private;
            field.InitExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(List<string>)));
            rtsResourceManager.Members.Add(field);
            
            
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            //代码风格:大括号的样式{}
            options.BracingStyle = "C";
            options.BlankLinesBetweenMembers = true;
            
            //输出文件路径

            string outputFile = Application.dataPath + "/Scripts/Gameplay/RTS/Generate/RTSResourceManager.cs";

            //保存

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFile))

            {

                //为指定的代码文档对象模型(CodeDOM) 编译单元生成代码并将其发送到指定的文本编写器，使用指定的选项。(官方解释)
                //将自定义代码编译器(代码内容)、和代码格式写入到sw中
                provider.GenerateCodeFromCompileUnit(unit, sw, options);

            }
        }
    }
}